#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=105,inf=1e12;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-48,ch=getchar();}
	return x*f;
}
int n,k,a[N],maxn;
map<int,int> mp;

int ans,tot;
vector<int> p[N];
void dfs(int pos){
	if(clock()*1.0/CLOCKS_PER_SEC>1.4){
		printf("%lld ",ans);
		exit(0);
	}
	if(pos==n+1){
		int res=0;
		for(int i=1;i<=k;i++){
			int mx=-inf,mn=inf;
			for(int x:p[i]) mx=max(mx,x),mn=min(mn,x);
			res+=mx-mn;
		}
		ans=min(ans,res);
		return;
	}
	for(int i=1;i<=tot;i++){
		if(p[i][p[i].size()-1]!=a[pos]&&p[i].size()<n/k){
			p[i].push_back(a[pos]);
			dfs(pos+1);
			p[i].pop_back();
		}
	}
	if(tot<k){
		p[++tot].push_back(a[pos]);
		dfs(pos+1);
		p[tot--].pop_back();		
	}
}

#define pii pair<int,int>
#define fi first
#define sd second

set<pii >s;
vector<pii> del;

signed main(){
	freopen("diyiti.in","r",stdin);
	freopen("diyiti.out","w",stdout);
	n=read(),k=read();
	for(int i=1;i<=n;i++) a[i]=read(),maxn=max(maxn,++mp[a[i]]);
	if(maxn>k){
		puts("-1");
		return 0;
	}
	sort(a+1,a+1+n);
	ans=inf;
	dfs(1);
	printf("%lld",ans);		
	return 0;
}
